import Cesium, { AssociativeArray, Viewer } from "../Ces/Cesium"
import WeImageryLayer from "./WeImageryLayer"

class WeLayerManager {
    private _viewer: Viewer
    private _layers: AssociativeArray
    private _weImageryLayers: AssociativeArray

    constructor(viewer) {
        this._viewer = viewer
        this._weImageryLayers = new Cesium.AssociativeArray()
    }

    get viewer() {
        return this._viewer
    }

    get weImageryLayers() {
        return this._weImageryLayers.values
    }

    addWeImageryLayer(options, id: string = null) {
        if (!Cesium.defined(id)) {
            id = Cesium.createGuid()
        }

        if (this._layers.contains(id)) {
            throw new Cesium.RuntimeError(
                'An layer with id ' + id + ' already exists in this collection.',
            )
        }

        const layer = new WeImageryLayer(options, id)
        this._weImageryLayers.set(id, layer)
        this._layers.set(id, layer)
        this.viewer.imageryLayers.add(layer)
        return layer
    }

    update(frameState) {
        for (let i = 0; i < this._layers.length; i++) {
            const layer = this._layers.values[i];
            if (Cesium.defined(layer.update)) {
                if (layer instanceof WeImageryLayer) {
                    layer.update(frameState)
                }
            }
        }
    }

    isDestroyed() {
        return false
    }

    destory() {
        return Cesium.destroyObject(this)
    }
}

export default WeLayerManager